//
//  Delegate.m
//  Skeleton
//
//  Created by Wolfgang Engel on 3/21/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

//#import "App.h"
#import "mfAppRunner.h"

//#include "Camera.h"

mfTouchApp *	touchApp;
id				appRunner;
int		windowOrientation;

//--------------------------------------
void mfRunApp(mfTouchApp *app){
	touchApp = app;
	app->mouseX = 0;
	app->mouseY = 0;
	
	// tie into glue
	setStartTime();
	setFrameNum(0);
}

//--------------------------------------
float ofGetFrameRate(){
	return ofFrameRate;
}

//--------------------------------------
void ofSetFrameRate(int targetRate)
{
	ofFrameRate = targetRate;
	[appRunner restartAnimationWithMultiplier:1.0f];
}

//--------------------------------------
int ofGetWidth()
{
	return width;
}

//--------------------------------------
int ofGetHeight()
{
	return height;
}

//--------------------------------------
int mfGetOrientation()
{
	return windowOrientation;
}

//--------------------------------------
void mfSetOrientation(int mode)
{
	if( mode == windowOrientation )
		return;
	
	windowOrientation = mode;
	
	[[UIApplication sharedApplication] setStatusBarOrientation: (mode==MF_LANDSCAPE?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait) animated: NO];
	
	if( mode==MF_LANDSCAPE ){
		width = 480;
		height = 320;
	}else{
		width = 320;
		height = 480;
	}
}

//--------------------------------------
void mfShowStatusBar()
{
	[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}

//--------------------------------------
void mfHideStatusBar()
{
	[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}



//--------------------------------------
@implementation mfAppRunner

+ (mfTouchApp*) getTouchApp
{
	return touchApp;
}

- (void) update
{

	touchApp->update();
	
	// setup view
	touchApp->view();
	
	// draw the app
	touchApp->draw();
	[_glView swapBuffers];
	
	// clean up the view
	touchApp->endView();
}


- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	// remember who we are
	appRunner = self;
	
	// default to a black status bar, even thought it's hidden
	[application setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
	
	// create a full-screen window
	CGRect	rect = [[UIScreen mainScreen] bounds];
	_window = [[UIWindow alloc] initWithFrame:rect];
	
	// create the OpenGL view and add it to the window
	// TODO - spruce this up
	_glView = [[mfInteractiveView alloc] initWithFrame:rect];
	//_glView = [[EAGLCameraView alloc] initWithFrame:rect pixelFormat:GL_RGB565_OES depthFormat:GL_DEPTH_COMPONENT16_OES preserveBackbuffer:NO];
	[_window addSubview:_glView];
	
	// show the window
	[_window makeKeyAndVisible];
	
	touchApp->setup();
		
	// create our rendering timer
	[self restartAnimationWithMultiplier:1.0f];
}


// Changes the frame rate when the application is about to be inactive.
- (void) applicationWillResignActive:(UIApplication *)application {
	NSLog(@"applicationWillResignActive:");
	
	//[self restartAnimationWithMultiplier:3.0f];
}

// Resumes the initial frame rate when the application becomes active.
- (void) applicationDidBecomeActive:(UIApplication *)application {
	NSLog(@"applicationDidBecomeActive:");
	
	//[self restartAnimationWithMultiplier:1.0f];

}

- (void) applicationWillTerminate:(UIApplication *)application
{
	touchApp->exit();
	delete touchApp;
	[_glView release];
	[_window release];
}

- (void) startAnimation 
{
	printf("startAnim \n");
	self->animator = [NSTimer scheduledTimerWithTimeInterval:(1.0f / ofFrameRate) target:self selector:@selector(update) userInfo:nil repeats:YES];
}

- (void) stopAnimation {
	
	printf("stopAnim \n");
	[self->animator invalidate];
	self->animator = nil;
}

- (void)restartAnimationWithMultiplier:(float)multiplier
{
	printf("restartAnim \n");
	
	if(self->animator != nil)
	{
		if([self->animator isValid])
			[self->animator invalidate];
		
		self->animator = nil;
	}
	
	self->animator = [NSTimer scheduledTimerWithTimeInterval:(multiplier / (ofFrameRate)) target:self selector:@selector(update) userInfo:nil repeats:YES];
}

- (void) dealloc
{
	touchApp->exit();
	delete touchApp;
	[_glView release];
	[_window release];
	[super dealloc];
}

@end
